import storage from '../../utils/storage';

const state = {
  device: 'desktop',
  size: storage.getOrDefault('size', 'medium')
}

const mutations = {
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_LANGUAGE: (state, language) => {
    state.language = language
    Cookies.set('language', language)
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  }
}

const actions = {
  appLoad({ rootGetters, dispatch }) {
      return new Promise(((resolve, reject) => {
          if (rootGetters.token) {
            dispatch("user/userInfo", null, {root: true}).then((res) => {
              dispatch("menu/permMenu", null, {root: true}).then((res) => {
                resolve()
              })
            })
          } else {
            resolve()
          }
      }))
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setLanguage({ commit }, language) {
    commit('SET_LANGUAGE', language)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
